package action;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Repeatable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.smile.beans.BeanUtils;
import org.smile.collection.CollectionUtils;
import org.smile.collection.LinkedHashMap;
import org.smile.collection.MapUtils;
import org.smile.collection.ResultMap;
import org.smile.commons.SmileRunException;
import org.smile.commons.ann.Note;
import org.smile.commons.ann.Param;
import org.smile.commons.ann.Resource;
import org.smile.commons.ann.Service;
import org.smile.http.HttpMethod;
import org.smile.io.IOUtils;
import org.smile.json.JSON;
import org.smile.json.JSONObject;
import org.smile.strate.action.SingtonActionSupport;
import org.smile.strate.ann.Action;
import org.smile.strate.ann.Controller;
import org.smile.strate.ann.GetMapping;
import org.smile.strate.ann.PostBody;
import org.smile.strate.ann.PostMapping;
import org.smile.strate.ann.RequestBody;
import org.smile.strate.ann.Result;
import org.smile.util.SysUtils;
import org.smile.validate.Field;
import org.smile.validate.Validate;
import org.smile.validate.ValidateType;

import org.smile.validate.ann.*;
import service.IStudentService;
import vo.Student;
@Note("学生信息管理")
@Service
@Controller(path="student",extend = "strate")
public class StudentAction extends SingtonActionSupport {
	@Resource
	private IStudentService studentService;
	private List<String> name;
	private Integer id;
	private Map<String, List<Date>> date;
	private InputStream file;
	private String fileFileName;
	private String fileFileType;
	private Object json;
	

	@Validate(fields = {
			@Field(name="name",type=ValidateType.required)
	})
	@Field(name="name",type=ValidateType.maxLength,value="5")
	@MaxLength(name="address",value="5")
	@Range(name="age",range={"1","10"})
	@Note("查询学生")
	@Action
	public Map findStudent1(Map<String,String> stu,
							@MinLength("3")
							String name,
							@NotEqual("5")
							Integer age,
							@Datetime
							String birthday,
							@Expression("age>5")
							String address,
							@Email
							String email,
							@Telephone
							String telephone,
							@Range(range = {"1","5"})
							int height){
		Map map=CollectionUtils.hashMap("name","胡真山");
		map.put("age", age);
		map.put("n", name);
		return map;
	}
	@Note("查询学生万贯")
	@Action(results={@Result(type="template",value="/action/template.html")})
	public String findStudentList(@Param(name="json")Student json){
		List list=new ArrayList();
		for(int i=0;i<10;i++){
			Map m=new HashMap();
			m.put("name","小折"+i);
			m.put("age",i);
			m.put("address","村庄"+i);
			list.add(m);
		}
		request().setAttribute("list", list);
//		JSONAware json=requestJsonBody();
		System.out.println(json);
		return SUCCESS;
	}
	@PostMapping
	public String findStudent(){
		this.json=CollectionUtils.hashMap("name","胡真山");
		return EXCEPTION;
	}
	
	
	
	@Action(results={@Result(type="freemarker",value="/WEB-INF/ftl/uploadFile.ftl")},method= {HttpMethod.GET,HttpMethod.POST})
	public String uploadFile() throws IOException{
		System.out.println(IOUtils.readString(file));
		setName(name);
		return SUCCESS;
	}
	
	@Action(results={@Result(type="freemarker",value="/WEB-INF/ftl/uploadFile.ftl")})
	public String uploadFiles(File file,String fileFileName) throws IOException{
		System.out.println(IOUtils.readLines(file,IOUtils.DEFAULT_ENCODE));
		request().setAttribute("fileFileName", fileFileName);
		return SUCCESS;
	}
	@PostBody(url="student")
	public Map putStudent(@RequestBody Student student,String name,int age) {
		json=student;
		return BeanUtils.mapFromBean(student);
	}

	@PostMapping(url="xmlBody")
	public Map xmlBody(int age,@RequestBody String xml,String name) {
		return MapUtils.hashMap("value",xml);
	}

	@PostBody(url="xmlBody2")
	public Map xmlBody2(String xml) {
		return MapUtils.hashMap("value",xml);
	}
	@PostMapping
	public Map testListMap(List<LinkedHashMap<String,String>> list){
		SysUtils.print(JSON.toJSONString(list.get(0)));
		return MapUtils.resultMap();
	}
	@GetMapping(url="student2")
	public Object getStudent(Long id) {
		return json;
	}
	
	@GetMapping(url="student/{name}/age-{age}/id-{id}.html")
	public Object getParamedStudent(String name,int age,int id) {
		Map map=new HashMap<>();
		map.put("name", name);
		map.put("age", age);
		map.put("id",id);
		return map;
	}

	public Object getJson() {
		return json;
	}

	public void setJson(Object json) {
		this.json = json;
	}

	public List<String> getName() {
		return name;
	}

	public void setName(List<String> name) {
		this.name = name;
	}

	public Map<String, List<Date>> getDate() {
		return date;
	}

	public void setDate(Map<String, List<Date>> date) {
		this.date = date;
	}


	public InputStream getFile() {
		return file;
	}

	public void setFile(InputStream file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getFileFileType() {
		return fileFileType;
	}

	public void setFileFileType(String fileFileType) {
		this.fileFileType = fileFileType;
	}
	
	
}
